﻿namespace Vcare.Controllers
{
  using System;
  using System.Web.Mvc;
  using Vcare.Models;
  using System.Configuration;
  using Vcare.DAL;
  using System.Data;

  public class PayPalController : Controller
  {

    private VcareContext db = new VcareContext();

    public ActionResult RedirectFromPaypal()
    {
      Members member = db.Members.Find(Session["id"]);
      member.DateOfPayment = DateTime.Now;
      member.Paid = true;
      db.Entry(member).State = EntityState.Modified;
      db.SaveChanges();
      return View(member);
    }

    public ActionResult CancelFromPaypal()
    {
      return View();
    }

    public ActionResult NotifyFromPaypal()
    {
      return View();
    }

    public ActionResult ValidateCommand(string product, string totalPrice, int memberid)
    {
      bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]);
      var paypal = new PayPalModel(useSandbox);
      paypal.item_name = product;
      paypal.amount = totalPrice;
      Members members = db.Members.Find(memberid);
      ViewBag.first_name = members.Firstname;
      ViewBag.last_name = members.Lastname;
      ViewBag.address1 = members.Address;
      ViewBag.city = members.Town;
      ViewBag.email = members.Email;
      ViewBag.zip = members.Postcode;
      ViewBag.phone = members.Phone;
      ViewBag.state = members.County;
      return View(paypal);
    }
  }
}
